<?php

class Modules_Daiting extends Module {

	public $name = "daiting";
	public $title = "Знакомства";

	protected function _actionproperties(Blocks $block) {
		
	}

	public function actions() {
		
	}

	public function mainActions() {
		return array(
			'list' => "Список анкет"
		);
	}

	public static function subMenu() {
		return array(
			'profiles' => array(
				'url' => "/admin.php?module=daiting&action=profiles",
				'title' => "Анкеты",
				'icon' => "/tpl/admin/images/list.png"
			)
		);
	}

	public function adminActionIndex() {
		return $this->adminActionProfiles();
	}

	public function adminActionProfiles() {
		$this->access("r");
		
		$profiles = Doctrine_Query::create()
				->from('DaitingProfiles p')
				->leftJoin('p.User')
				->execute();
		
		$_profile = new DaitingProfiles();
		
		$ids = array();
		foreach($profiles as $profile) $ids[] = $profile->user_id;
		
		$users = Doctrine_Query::create()
				->from("Users u")
				->whereNotIn("u.id", $ids)
				->execute();

		self::$mainTitle = "Список анкет";
		
		return $this->renderTpl('profiles_list.html', array(
			'profiles' => $profiles,
			'_profile' => $_profile,
			'users' => $users,
		));
	}
	
	public function adminActionProfile_save(){
		$this->access("c");
		
		$data = isset($_REQUEST['profile']) ? $_REQUEST['profile'] : array();
		$data['id'] = (int) $data['id'];
		
		$profile = $data['id'] ? DaitingProfilesTable::getInstance()->find($data['id']) : new DaitingProfiles();
		unset($data['id']);
		
		$profile->fromArray($data);
		$profile->save();
		
		Request::redirect(-1);
	}
	
	public function adminActionProfile_delete(){
		$this->access("d");
		
		$id = !empty($_REQUEST['id']) ? (int)$_REQUEST['id'] : 0;
		if(!$id) Request::redirect (404);
		
		$profile = Doctrine_Query::create()->from('DaitingProfiles p')->where('p.id = ?', $id)->fetchOne();
		if(!$profile) Request::redirect (404);
		
		return $profile->delete();
	}
}